<?php
 function https_request($url, $data = null){
     $curl = curl_init();
     curl_setopt($curl, CURLOPT_URL, $url);
     curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
     curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);

     //参数不为空则为post请求

     if (!empty($data)){

         curl_setopt($curl, CURLOPT_POST, 1);
         curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
     }
     curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
     $output = curl_exec($curl);
     curl_close($curl);
     return $output;
 }

// 生成无重复订单号
function randNumber($prefix_name = ''){
    $orderSn = strtoupper(dechex(date('m'))) . date('d') . substr(time(), -5) . substr(microtime(), 2, 3) . sprintf('%02d', rand(0, 99));

    $danhao = date('Ymd').substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 5);
    return $prefix_name . $danhao . $orderSn;
}

function getRealIp(){
    $ip=false;
    if(!empty($_SERVER["HTTP_CLIENT_IP"])){
        $ip = $_SERVER["HTTP_CLIENT_IP"];
    }
    if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
        $ips = explode (", ", $_SERVER['HTTP_X_FORWARDED_FOR']);
        if ($ip) { array_unshift($ips, $ip); $ip = FALSE; }
        for ($i = 0; $i < count($ips); $i++) {
            if (!eregi ("^(10│172.16│192.168).", $ips[$i])) {
                $ip = $ips[$i];
                break;
            }
        }
    }
    return ($ip ? $ip : $_SERVER['REMOTE_ADDR']);
}

function guid(){
    if (function_exists('com_create_guid')){
        return com_create_guid();
    }else{
        mt_srand((double)microtime()*10000);//optional for php 4.2.0 and up.
        $charid = strtoupper(md5(uniqid(rand(), true)));
        $hyphen = chr(45);// "-"
        $uuid = chr(123)// "{"
            .substr($charid, 0, 8).$hyphen
            .substr($charid, 8, 4).$hyphen
            .substr($charid,12, 4).$hyphen
            .substr($charid,16, 4).$hyphen
            .substr($charid,20,12)
            .chr(125);// "}"
        return $uuid;
    }
}

function curl_json_post($url,$data){
    if(is_array($data)){
        $data_string = json_encode($data);
    }else{
        $data_string = $data;
    }
    // echo $data_string;die;
    $ch = curl_init($url);
    // curl_setopt($ch, CURLOPT_TIMEOUT,5);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_POSTFIELDS,$data_string);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/json',
            'Content-Length: ' . strlen($data_string))
    );
    $result = curl_exec($ch);
    curl_close ( $ch );
    return $result;
}

function BankType($pay_type){
    $pay_type = strtolower($pay_type);
    switch ($pay_type){
        case 'weixinscancode':
            return 'WeixinScancode';
        case 'qq_pay':
            return 'qq_pay';
        case 'bank_pay':
            return 'bank_pay';
        case 'alipay_pay':
            return 'alipay_pay';
        case 'weixingzh_pay':
            return 'weixinGzh_pay';
        case 'jd_pay':
            return 'jd_pay';
    }
    exit(json_encode(array('error' => 8 , 'msg' => '传入的支付类型不正确'),JSON_UNESCAPED_UNICODE));
}

function curl_post($url, $data){
    $data = http_build_query($data);
    // file_put_contents('./tmp.txt',$data);
    //echo '<br>'.$data.'<br>';
    // mb_strlen($str,'utf-8');
    // var_dump((mb_strlen($data,'utf-8') == strlen($data)));
    $opts = array(
        'http'=>array(
            'method'=>"POST",
            'timeout'=>60,
            'header'=>"Content-type: application/x-www-form-urlencoded\r\n".
                "Content-length:".mb_strlen($data,'utf-8')."\r\n" .
                "Cookie: foo=bar\r\n" .
                "\r\n",
            'content' => $data,
        )
    );
    $cxContext = stream_context_create($opts);
    $result = file_get_contents($url, false, $cxContext);
    return $result;
}

function Putdd($TextName , $data){
    file_put_contents('./' . $TextName , var_export($data,true));
}

// 本程序接口自己的Sign
function SetMySign($SignData,$key){
    $str = "";
    foreach ($SignData as $key1 => $value) {
        $str = $str . $key1 . '=' . $value . '&';
    }

    $StrsignTemp = rtrim($str, '&');
    $MySign = md5($StrsignTemp . '&key=' . $key);
    return strtoupper($MySign);
}

/* 异步通知状态提示 */
// 1. 充值方要求返回的标识，本接口要求成功充值的信息
// 参数2是适用方返回的异步通知标识
// 如 犇付的是 ok  那么 $channelMsg = ok
function NotifyechoMessage($channelMsg,$mymsg){
    if($mymsg != 'SUCCESS') {
        return 'fail';
    }else{
        return $channelMsg;
    }
}